﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Data
{
    public class DataProvider:UserInfor
    {
        protected static string _connectString;
        protected SqlConnection Cnn;
        protected SqlDataAdapter da;
        protected SqlCommand cmd;

        public static string ConnectionString
        {
            get { return _connectString; }
            set { _connectString = value; }
        }

        public void connect()
        {
            Cnn = new SqlConnection(ConnectionString);
            Cnn.Open();
        }

        public void Disconnect()
        {
            Cnn.Close();
        }
        public DataTable getDSUser(string strStore)
        {
            try
            {
                cmd = new SqlCommand(strStore, Cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                DataTable ds = new DataTable();
                da.Fill(ds);
                return ds;
            }
            catch (Exception sqlex)
            {
                throw sqlex;
            }
            finally
            {
                Cnn.Dispose();
            }
        }
        public DataSet layalluser(string strStore)
        {
            try
            {
                cmd = new SqlCommand(strStore, Cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception sqlex)
            {
                throw sqlex;
            }
            finally
            {
                Cnn.Dispose();
            }
        }
        public void Themnguoidung(string Email, string Matkhau, string Hoten, string Diachi, string Sodt, string Sofax, int MaKieuNguoiDung, string strStore)
        {
            try
            {
                cmd = new SqlCommand(strStore, Cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email;
                cmd.Parameters.Add("@MatKhau", SqlDbType.VarChar).Value = Matkhau;
                cmd.Parameters.Add("@HoTen", SqlDbType.NVarChar).Value = Hoten;
                cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar).Value = Diachi;
                cmd.Parameters.Add("@SoDT", SqlDbType.VarChar).Value = Sodt;
                cmd.Parameters.Add("@SoFax", SqlDbType.VarChar).Value = Sofax;
                cmd.Parameters.Add("@MaKieuNguoiDung", SqlDbType.Int).Value = MaKieuNguoiDung;
                cmd.ExecuteNonQuery();
            }
            catch (SqlException sqlex)
            {
                throw sqlex;
            }
            finally
            {
                Cnn.Dispose();
            }
        }
        public void SuaNguoiDung(string Email,string Matkhau, string Hoten, string Diachi, string Sodt, string Sofax, int MaKieuNguoiDung, string strStore)
        {
            try
            {
                cmd = new SqlCommand(strStore, Cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email;
                cmd.Parameters.Add("@MatKhau", SqlDbType.VarChar).Value = Matkhau;
                cmd.Parameters.Add("@HoTen", SqlDbType.NVarChar).Value = Hoten;
                cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar).Value = Diachi;
                cmd.Parameters.Add("@SoDT", SqlDbType.VarChar).Value = Sodt;
                cmd.Parameters.Add("@SoFax", SqlDbType.VarChar).Value = Sofax;
                cmd.Parameters.Add("@MaKieuNguoiDung", SqlDbType.Int).Value = MaKieuNguoiDung;
                cmd.ExecuteNonQuery();
            }
            catch (SqlException sqlex)
            {
                throw sqlex;
            }
            finally
            {
                Cnn.Dispose();
            }
        }
        public void XoaNguoiDung(string Email, string strStore)
        {
            try
            {
                cmd = new SqlCommand(strStore, Cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = Email;
                cmd.ExecuteNonQuery();
            }
            catch (SqlException sqlex)
            {
                throw sqlex;
            }
            finally
            {
                Cnn.Dispose();
            }
        }
        public DataSet getDSNSX(string strStore)
        {
            try
            {
                cmd = new SqlCommand(strStore, Cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception sqlex)
            {
                throw sqlex;
            }
            finally
            {
                Cnn.Dispose();
            }
        }
        private NhasxInfor _nhasx;
        public NhasxInfor nhasx
        {
            get { return _nhasx; }
            set { _nhasx = value; }
        }
        //public void ThemNSX()
        //{
        //    SqlCommand cmd = new SqlCommand();
        //    cmd.Connection = Cnn;
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    cmd.CommandText = "NhaSX_Insert";
        //    cmd.Parameters.Add("@MaNSX", SqlDbType.VarChar).Value = nhasx.Mansx;
        //    cmd.Parameters.Add("@TenNSX", SqlDbType.NVarChar).Value = nhasx.Tennsx;
        //    cmd.ExecuteNonQuery();
        //}
    }
}
    